﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using System.IO;
namespace MegavaniaXLevelEditor
{
    public class TextureRepo
    {
        private IDictionary<string, IList<Texture2D>> TextureDict = new Dictionary<string, IList<Texture2D>>();

        public void AddTexture(string dictname, Texture2D tex)
        {
            if (!TextureDict.ContainsKey(dictname))
            {
                TextureDict[dictname] = new List<Texture2D>();
            }
            if (!TextureDict[dictname].Contains(tex))
                TextureDict[dictname].Add(tex);
        }


        internal IList<Texture2D> GetTextureList(string CurrentTileSubset)
        {
            if (!TextureDict.ContainsKey(CurrentTileSubset))
            {
                return new List<Texture2D>(); // empty list
            }
            else
            {
                return TextureDict[CurrentTileSubset];
            }
        }


        internal string AddTilesToContent(ContentBuilder cb, ContentManager cm)
        {          
            // it's all hardcoded, aww yeah

            string basedir = @"D:\Megavania\content\";

            string[] subdirs = 
                new string[1] 
                { 
                    @"levels\magnet\" 
                };

            cb.Clear();

            string be = string.Empty;
            
            foreach (string subdir in subdirs)
            {
                var fnames = new List<string>();
                DirectoryInfo di = new DirectoryInfo(basedir + subdir);
                FileInfo[] files = di.GetFiles("*.png");
                foreach (var file in files)
                {
                    string fname = file.Name;
                    fnames.Add(fname);
                    cb.Add(file.FullName, fname, null, "TextureProcessor");         
                }

                be += cb.Build();
                foreach (var fname in fnames)
                {
                    AddTexture(subdir, cm.Load<Texture2D>(fname));
                }

            }
            /*
            //cb            .Add(basedir + @"levels\magnet\floor1.png", "tex", null, "TextureProcessor");
            string buildError = cb.Build();

            if (string.IsNullOrEmpty(buildError))
            {
                AddTexture("magnet", cm.Load<Texture2D>("tex"));
                return string.Empty;
            }
            else
            {
                return buildError;
            }*/
            return be;
        }

    }
}
